package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.vo.BillDetailRecordVo;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.BillDetailRecordMapper;
import com.canaan.business.domain.bill.BillDetailRecord;
import com.canaan.business.service.bill.IBillDetailRecordService;
import com.canaan.common.annotation.TableName;
/**
 * 订单详细纪录Service业务层处理
 *
 * @author canaan
 * @date 2025-03-28
 */
@Service
public class BillDetailRecordServiceImpl implements IBillDetailRecordService {
    @Resource
    private BillDetailRecordMapper billDetailRecordMapper;

    /**
     * 查询订单详细纪录
     *
     * @param id 订单详细纪录主键
     * @return 订单详细纪录
     */
    @Override
    public BillDetailRecord selectBillDetailRecordById(Long id) {
        return billDetailRecordMapper.selectBillDetailRecordById(id);
    }

    /**
     * 查询订单详细纪录列表
     *
     * @param billDetailRecord 订单详细纪录
     * @return 订单详细纪录
     */
    @Override
    public List<BillDetailRecord> selectBillDetailRecordList(BillDetailRecord billDetailRecord) {
        return billDetailRecordMapper.selectBillDetailRecordList(billDetailRecord);
    }

    @Override
    public List<BillDetailRecordVo> selectBillDetailRecordListPageDatas(BillDetailRecord billDetailRecord) {
        return billDetailRecordMapper.selectBillDetailRecordListPageDatas(billDetailRecord);
    }

    /**
     * 新增订单详细纪录
     *
     * @param billDetailRecord 订单详细纪录
     * @return 结果
     */
    @Override
    public int insertBillDetailRecord(BillDetailRecord billDetailRecord) {
        billDetailRecord.setCreateBy(SecurityUtils.getUsername());
        billDetailRecord.setCreateTime(DateUtils.getNowDate());
        return billDetailRecordMapper.insertBillDetailRecord(billDetailRecord);
    }

    /**
     * 批量新增订单详细纪录
     *
     * @param billDetailRecordList 订单详细纪录列表
     * @return 结果
     */
    @Override
    public int batchInsertBillDetailRecord(List<BillDetailRecord> billDetailRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (BillDetailRecord billDetailRecord : billDetailRecordList){
            billDetailRecord.setCreateBy(username);
            billDetailRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<billDetailRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > billDetailRecordList.size()) {
                endIndex = billDetailRecordList.size();
            }
            insertNum = insertNum + billDetailRecordMapper.batchInsertBillDetailRecord(billDetailRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改订单详细纪录
     *
     * @param billDetailRecord 订单详细纪录
     * @return 结果
     */
    @Override
    public int updateBillDetailRecord(BillDetailRecord billDetailRecord) {
        billDetailRecord.setUpdateBy(SecurityUtils.getUsername());
        billDetailRecord.setUpdateTime(DateUtils.getNowDate());
        return billDetailRecordMapper.updateBillDetailRecord(billDetailRecord);
    }

    /**
     * 批量修改订单详细纪录
     *
     * @param billDetailRecordList 订单详细纪录列表
     * @return 结果
     */
    @Override
    public int batchUpdateBillDetailRecord(List<BillDetailRecord> billDetailRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (BillDetailRecord billDetailRecord : billDetailRecordList){
            billDetailRecord.setUpdateBy(username);
            billDetailRecord.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<billDetailRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > billDetailRecordList.size()) {
                endIndex = billDetailRecordList.size();
            }
            updateNum = updateNum + billDetailRecordMapper.batchUpdateBillDetailRecord(billDetailRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除订单详细纪录
     *
     * @param ids 需要删除的订单详细纪录主键集合
     * @return 结果
     */
    @Override
    public int deleteBillDetailRecordByIds(Long[] ids) {
        return billDetailRecordMapper.deleteBillDetailRecordByIds(ids);
    }

    /**
     * 删除订单详细纪录信息
     *
     * @param id 订单详细纪录主键
     * @return 结果
     */
    @Override
    public int deleteBillDetailRecordById(Long id) {
        return billDetailRecordMapper.deleteBillDetailRecordById(id);
    }
}
